import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

class Solution {
    public int lastStoneWeight(int[] stones) {
        Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for(int i = 0; i < stones.length; i++){
            queue.offer(stones[i]);
        }
        while(queue.size() > 1){
            int value1 = queue.poll();
            int value2 = queue.poll();
            if(value1 != value2){
                queue.offer(value1 - value2);
            }
        }
        if(queue.isEmpty()){
            return 0;
        }
        return queue.poll();
    }
}